package com.itheima.blog.web.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.api.client.User.UserClient;
import com.itheima.api.client.entity.WebUser;
import com.itheima.blog.web.domain.po.TComment;
import com.itheima.blog.web.dao.TCommentMapper;
import com.itheima.blog.web.domain.po.TComment;
import com.itheima.blog.web.service.ITCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.domain.PageResDto;
import com.itheima.common.domain.R;
import com.itheima.common.domain.dto.CommentPageDto;
import com.itheima.common.domain.po.ResComment;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


import com.itheima.blog.util.BeanHelper;
import com.itheima.blog.util.UserThreadLocalUtils;
import com.itheima.blog.web.domain.vo.CommentVO;


import com.itheima.blog.web.service.ITBlogService;

import com.itheima.common.domain.PageReqDto;
import com.itheima.common.domain.PageResDto;


/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author wc
 * @since 2023-07-11
 */
@Service
@RequiredArgsConstructor
public class TCommentServiceImpl extends ServiceImpl<TCommentMapper, TComment> implements ITCommentService {
    private final UserClient userClient;
    @Override
    public R getBlogComment(Long blogId) {
        if (blogId!=null){
            List<TComment> list = lambdaQuery().eq(TComment::getBlogId, blogId).isNull(TComment::getCommentId).list();
            return R.ok(list);
        }else {
            return R.error("无法获取评论");
        }

    }

    @Override
    public R getRejectComment(Long blogId, Long commentId) {
        if (blogId!=null&&commentId!=null){
            List<TComment> list = lambdaQuery().eq(TComment::getBlogId, blogId).eq(TComment::getCommentId, commentId).list();
            return R.ok(list);
        }else {
            return R.error("无法获取二级评论");
        }

    }

    @Override
    public PageResDto<ResComment> getCommentList(CommentPageDto commentPageDto) {
        Integer page = commentPageDto.getPage();
        Integer size = commentPageDto.getSize();
        String userName = commentPageDto.getUserName();
        //todo userName转userId
        WebUser user = new WebUser();
        if (userName!=null){
            user = userClient.getWebUserByName(userName);
        }
        String content = commentPageDto.getContent();
        String type = commentPageDto.getType();
        IPage<TComment> tCommentIPage = lambdaQuery()
                .eq(content != null, TComment::getContent, content)
                .eq(StringUtils.isNotBlank(userName),TComment::getUserId,user.getId())
                .page(new Page<>()).setSize(size).setPages(page);
        List<TComment> records = tCommentIPage.getRecords();
//        Map<String, TComment> userId = records.stream().collect(Collectors.toMap(TComment::getUserId, tComment -> tComment));
//        Map<String, TComment> targetUserId = records.stream().collect(Collectors.toMap(TComment::getTargetUserId, t -> t));
        List<ResComment> list = new ArrayList<>();
        for (TComment record : records) {
            String image = userClient.getWebUser(Integer.parseInt(record.getUserId())).getImage();
            String u_name = userClient.getWebUser(Integer.parseInt(record.getUserId())).getName();
            String tar_uname = userClient.getWebUser(Integer.parseInt(record.getTargetUserId())).getName();
            ResComment resComment = new ResComment();
            BeanUtils.copyProperties(record,resComment);
            resComment.setImgUrl(image);
            resComment.setUserName(u_name);
            resComment.setTargetUserName(tar_uname);
            resComment.setType("1");
            list.add(resComment);
        }
        Long pages = tCommentIPage.getPages();
        Long size1 = tCommentIPage.getSize();
        Long total = tCommentIPage.getTotal();
        PageResDto<ResComment> resCommentPageResDto = new PageResDto<>();
        resCommentPageResDto.setData(list);
        resCommentPageResDto.setCurrentPage(pages.intValue());
        resCommentPageResDto.setTotal(total.intValue());
        resCommentPageResDto.setSize(size1.intValue());
        return resCommentPageResDto;
    }

    @Override
    public void deleteCommentByIds(List<Integer> ids) {
        this.deleteCommentByIds(ids);
        for (Integer id : ids) {
            lambdaUpdate().eq(TComment::getCommentId,id).remove();
        }
    }
    @Override
    public Long getComments() {
        long count = count();
        return count;
    }



    @Override
    public PageResDto<CommentVO> myCommentPage(PageReqDto pageReqDto) {
        Integer webUserId = UserThreadLocalUtils.getWebUserId();
        String name = userClient.getWebUser(webUserId).getName();
        Page<TComment> page = new Page<>();
        page.setSize(pageReqDto.getSize());
        page.setPages(pageReqDto.getPage());
        Page<TComment> commentPage = lambdaQuery()
                .eq(TComment::getUserId, webUserId)
                .eq(TComment::getTargetCommentId,null)
                .page(page);
        List<TComment> records = commentPage.getRecords();
        if (records.isEmpty()){
            return null;
        }
        List<CommentVO> commentVOList = BeanHelper.copyWithCollection(records, CommentVO.class);
        for (CommentVO commentVO : commentVOList) {
            commentVO.setUserName(name);
        }
        PageResDto<CommentVO> commentVOPageResDto = new PageResDto<>(pageReqDto,commentPage.getTotal(),commentVOList);
        return commentVOPageResDto;
    }

    @Override
    public PageResDto<CommentVO> otherCommentPage(PageReqDto pageReqDto) {
        Integer webUserId = UserThreadLocalUtils.getWebUserId();
        Page<TComment> page = new Page<>();
        page.setSize(pageReqDto.getSize());
        page.setPages(pageReqDto.getPage());
        Page<TComment> commentPage = lambdaQuery()
                .eq(TComment::getTargetUserId, webUserId)
                .page(page);
        List<TComment> records = commentPage.getRecords();
        if (records.isEmpty()){
            return null;
        }
        String name=null;
        for (TComment record : records) {
            String targetUserId = record.getTargetUserId();
            Integer valueOf = Integer.valueOf(targetUserId);
            name = userClient.getWebUser(valueOf).getName();
        }
        List<CommentVO> commentVOList = BeanHelper.copyWithCollection(records, CommentVO.class);
        for (CommentVO commentVO : commentVOList) {
            commentVO.setUserName(name);
        }
        PageResDto<CommentVO> commentVOPageResDto = new PageResDto<>(pageReqDto,commentPage.getTotal(),commentVOList);
        return commentVOPageResDto;
    }

    @Override
    public void rmMyComment(Long id) {
        List<TComment> list = lambdaQuery().eq(TComment::getTargetCommentId, id).list();
        removeBatchByIds(list);
        removeById(id);
    }
}
